package com.kehao.accountbook.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.kehao.accountbook.AccountApplication;
import com.kehao.accountbook.R;
import com.kehao.accountbook.adapter.ExpenditureAdapter;
import com.kehao.accountbook.chart.DrawLineChart;
import com.kehao.accountbook.chart.DrawPieChart;
import com.kehao.accountbook.pojo.Expenditure;
import com.kehao.accountbook.pojo.SimpleDate;

import org.litepal.LitePal;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChartFragment extends Fragment implements View.OnClickListener {
    EditText et_year;
    EditText et_month;
    Button btn_query;
    TextView tv_pay;
    TextView tv_get;
    List<Expenditure> list;
    LineChart lineChart;
    PieChart pieChartPay;
    PieChart pieChartGet;
    ImageView img_line;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_chart, container, false);
        SimpleDate todayDate = new SimpleDate();
        et_year = view.findViewById(R.id.et_year);
        et_month = view.findViewById(R.id.et_month);
        et_year.setText(todayDate.getYear()+"");
        et_month.setText(todayDate.getMonth()+"");
        btn_query = view.findViewById(R.id.btn_query);
        btn_query.setOnClickListener(this);
        tv_pay = view.findViewById(R.id.tv_pay);
        tv_get = view.findViewById(R.id.tv_get);
        lineChart = view.findViewById(R.id.linechart);
        pieChartPay = view.findViewById(R.id.piechart_pay);
        pieChartGet = view.findViewById(R.id.piechat_get);
        img_line = view.findViewById(R.id.img_line);
        return view;
    }

    @Override
    public void onClick(View v) {
        lineChart.setVisibility(View.VISIBLE);
        pieChartGet.setVisibility(View.VISIBLE);
        pieChartPay.setVisibility(View.VISIBLE);
        img_line.setVisibility(View.VISIBLE);
        switch (v.getId()){
            case R.id.btn_query:
                SimpleDate date = new SimpleDate(Integer.parseInt(et_year.getText().toString()),
                        Integer.parseInt(et_month.getText().toString()));
                if(list!=null){
                    list.clear();
                }
                //在网络上同步数据
                getAccountApplication().syncData(date.getYear(),date.getMonth());
                //再查询出来
                list =  LitePal.where("month = ? and year = ? and username = ? and status != ?",Integer.toString(date.getMonth()),
                        Integer.toString(date.getYear()),getAccountApplication().getUsername(),Integer.toString(Expenditure.STATUS_REMOVE))
                        .order("day , hour , min , second")
                        .find(Expenditure.class);
                //整理数据
                int monthday = SimpleDate.getMonthDay(date.getYear(),date.getMonth());
                double[] pay = new double[monthday];
                double[] get = new double[monthday];
                double paynormal,payshopping,payeatting,payfinancial;
                paynormal = payshopping = payeatting = payfinancial = 0;
                double getnormal,getshopping ,geteatting ,getfinancial;
                getnormal = getshopping = geteatting = getfinancial =0;
                double max = 0;
                for(int i = 0 ; i<pay.length;i++){
                    get[i]=pay[i]=0;
                }
                for(Expenditure e :list){
                    if(e.getMoney()>0){
                        get[e.getDay()-1] += e.getMoney();
                        if (e.getMoney()>max){
                            max = e.getMoney();
                        }
                        switch (e.getType()){
                            case Expenditure.TYPE_EATTING:
                                geteatting+=e.getMoney();
                                break;
                            case Expenditure.TYPE_FINANCIAL:
                                getfinancial += e.getMoney();
                                break;
                            case Expenditure.TYPE_NORMAL:
                                getnormal +=e.getMoney();
                                break;
                            case Expenditure.TYPE_SHOPPING:
                                getshopping += e.getMoney();
                                break;
                        }
                    }else {
                        pay[e.getDay()-1] -= e.getMoney();
                        if(e.getMoney()<-max){
                            max = -e.getMoney();
                        }
                        switch (e.getType()){
                            case Expenditure.TYPE_EATTING:
                                payeatting-=e.getMoney();
                                break;
                            case Expenditure.TYPE_FINANCIAL:
                                payfinancial -=e.getMoney();
                                break;
                            case Expenditure.TYPE_NORMAL:
                                paynormal -=e.getMoney();
                                break;
                            case Expenditure.TYPE_SHOPPING:
                                payshopping -= e.getMoney();
                                break;
                        }
                    }
                }//此时两个pay,get,max全部都为非负数
                //显示linechart
                DrawLineChart drawLineChart = new DrawLineChart(lineChart);
                drawLineChart.draw(monthday,(((int)max)/100+1)*100,pay,get,"支出","收入");
                //显示piechart
                DrawPieChart drawPieChartPay = new DrawPieChart(pieChartPay);
                drawPieChartPay.draw("支出比例",paynormal,payshopping,payeatting,payfinancial);
                DrawPieChart drawPieChartGet = new DrawPieChart(pieChartGet);
                drawPieChartGet.draw("收入比例",getnormal,getshopping,geteatting,getfinancial);
                updatePayAndGet(list);
                break;
        }
    }

    private void updatePayAndGet(List<Expenditure> list) {
        double pay = 0;
        double get = 0;
        for (Expenditure i :list){
            double money = i .getMoney();
            if(money>0){
                get+=money;
            }else if(money<0){
                pay+=money;
            }
        }
        if(pay!=0){
            tv_pay.setText("月支出: "+(-pay));
        }else {
            tv_pay.setText("月支出：暂无支出");
        }
        if(get!=0){
            tv_get.setText("月收入： "+get);
        }else {
            tv_get.setText("月收入： 暂无收入");
        }
    }

    private AccountApplication getAccountApplication(){
        return (AccountApplication)getActivity().getApplication();
    }

}
